package com.llc.spring.cloud.sdk.core;

import com.llc.spring.cloud.sdk.core.http.GatewayDefine;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

/**
 * time 2018/8/27 18:47
 *
 * @author xiaojie.zhu <br>
 */
public class DefaultGatewaySelector implements GatewaySelector {

    private Random r = new Random();

    @Override
    public String getGatewayUrl() {
        List<List<String>> lists = new ArrayList<>();
        lists.add(GatewayDefine.getUserGateway());
        lists.add(getGatewayFromEnv());
        try {
            List<String> gatewayFromFile = getGatewayFromFile();
            lists.add(gatewayFromFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        String serverUrl = null;
        Iterator<List<String>> iterator = lists.iterator();
        while (serverUrl == null && iterator.hasNext()){
            List<String> stringList = iterator.next();
            serverUrl = random(stringList);
        }

        return serverUrl;
    }


    private String random(List<String> gateways){
        if(gateways != null && gateways.size() > 0){
            int i = r.nextInt(gateways.size());
            return gateways.get(i);
        }else{
            return null;
        }
    }



    private static final DefaultGatewaySelector INSTANCE = new DefaultGatewaySelector();

    public static DefaultGatewaySelector getInstance(){
        return INSTANCE;
    }

    private static List<String> getGatewayFromFile() throws IOException {
        List<String> gateUrlList = new ArrayList<>();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(DefaultGatewaySelector.class.getResourceAsStream("/META-INF/gateway")));
            String line;
            while ((line = reader.readLine()) != null){
                String lineTrim = line.trim();
                if(!line.startsWith("#")){
                    if(!"".equals(lineTrim)){
                        gateUrlList.add(lineTrim);
                    }
                }
            }
        } finally {
            if(reader != null){
                reader.close();
            }
        }

        return gateUrlList;
    }

    private static List<String> getGatewayFromEnv(){
        String text = System.getProperty("gateway");
        if(text == null){
            return null;
        }else{
            String[] split = text.split(",");
            if(split.length <= 0){
                return null;
            }else{
                List<String> urls = null;
                for (String s : split) {
                    String trimUrl = s.trim();
                    if(!"".equals(trimUrl)){
                        if(urls == null){
                            urls = new ArrayList<>();
                        }
                        urls.add(trimUrl);
                    }
                }

                return urls;
            }
        }

    }


}
